package com.ailatrieuphu.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.ailatrieuphu.CMD;
import com.ailatrieuphu.Connection;
import com.ailatrieuphu.IAction;
import com.ailatrieuphu.Message;
import com.ailatrieuphu.MyActivity;
import com.ailatrieuphu.R;
import com.ailatrieuphu.dialog.DialogWaitting;
import com.ailatrieuphu.util.PreferenceUtils;

public class ActivityLogin extends MyActivity implements OnClickListener {
	private String account = "", password = "";
	private boolean isConnecting;
	private EditText etAccount, etPassword;
	private Button btnLogin, btnRegister;
	private CheckBox cbRemember;
	private DialogWaitting dialogWaitting;

	private Button register_btnRegister;
	private EditText register_etAccount;
	private EditText register_etPassword;
	private EditText register_etRePassword;
	String register_account, register_password;
	View layoutLogin, layoutRegister;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		layoutLogin = View.inflate(this, R.layout.activity_login, null);
		layoutRegister = View.inflate(this, R.layout.activity_register, null);

		setContentView(layoutLogin);

		doConnect();

		account = PreferenceUtils.getString(this, "account", "account");
		password = PreferenceUtils.getString(this, "password", "password");
		isConnecting = PreferenceUtils.getBoolean(this, "isConnecting",
				"isConnecting");
		etAccount = (EditText) layoutLogin.findViewById(R.id.login_etAccount);
		etPassword = (EditText) layoutLogin.findViewById(R.id.login_etPassword);
		cbRemember = (CheckBox) layoutLogin.findViewById(R.id.login_cbRemember);

		btnLogin = (Button) layoutLogin.findViewById(R.id.login_btnLogin);
		btnRegister = (Button) layoutLogin.findViewById(R.id.login_btnRegister);

		register_btnRegister = (Button) layoutRegister
				.findViewById(R.id.register_btnRegister);
		register_etAccount = (EditText) layoutRegister
				.findViewById(R.id.register_etAccount);
		register_etPassword = (EditText) layoutRegister
				.findViewById(R.id.register_etPassword);
		register_etRePassword = (EditText) layoutRegister
				.findViewById(R.id.register_etRePassword);
		register_btnRegister.setOnClickListener(this);

		btnLogin.setOnClickListener(this);
		btnRegister.setOnClickListener(this);

		etAccount.setText(account);
		etPassword.setText(password);
		cbRemember.setChecked(isConnecting);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	public void C_LOGIN() {
		Message message = new Message(CMD.C_LOGIN);
		message.writeString(account);
		message.writeString(password);
		System.out.println("C_Login: " + account + " - " + password);
		connection.sendMessage(message);
	}

	public void S_LOGIN(Message message) {
		dialogWaitting.dismiss();
		boolean check = message.readBoolean();
		String s = message.readString();
		if (check) {
			if (!cbRemember.isChecked()) {
				etAccount.setText("");
				etPassword.setText("");
				cbRemember.setChecked(false);
			}
			PreferenceUtils.putString(this, "account", etAccount.getText()
					.toString(), "account");
			PreferenceUtils.putString(this, "password", etPassword.getText()
					.toString(), "password");
			PreferenceUtils.putBoolean(this, "isConnecting",
					cbRemember.isChecked(), "isConnecting");
			showToast(s);
			startActivity(ActivityRoom.class);
			finish();
		} else {
			showToast("Tai khoan hoac mat khau khong hop le");
			return;
		}
	}

	public void C_REGISTER(String account, String password) {
		Message message = new Message(CMD.C_REGISTER);
		message.writeString(account);
		message.writeString(password);
		connection.sendMessage(message);
	}

	private boolean checkLogin(String account, String password) {
		boolean check = true;
		if (account.length() < 2 || password.length() < 1
				|| password.contains(" ") || account.contains(" ")) {
			showToast("Tai khoan va mat khau khong hop le");
			return false;
		}
		return check;
	}

	private boolean checkRegister(String account, String password,
			String rePassword) {
		if (account.equals("") || password.equals("") || password.contains(" ")
				|| account.contains(" ") || rePassword.contains(" ")
				|| rePassword.equals("")) {
			showToast("Thong tin dang ky khong hop le, vui long nhap lai");
			return false;
		}
		if (!rePassword.equals(password)) {
			showToast("Mat khau xac nhan khong dung");
			return false;
		}
		if (account.length() < 2) {
			showToast("Tai khoan phai co do dai lon hon 2");
			return false;
		}
		if (password.length() < 1) {
			showToast("Mat khau phai co do dai lon hon 2");
			return false;
		}
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.login_btnLogin:
			account = etAccount.getText().toString();
			password = etPassword.getText().toString();
			if (checkLogin(account, password)) {
				dialogWaitting = DialogWaitting
						.createAndShowDialogWaiting(this);
				C_LOGIN();
			}
			break;

		case R.id.login_btnRegister:
			setContentView(layoutRegister);
			break;
		case R.id.register_btnRegister:
			// Dang ky
			register_account = register_etAccount.getText().toString();
			register_password = register_etPassword.getText().toString();
			String repass = register_etRePassword.getText().toString();
			if (checkRegister(register_account, register_password, repass)) {
				C_REGISTER(register_account, register_password);
			}
			break;

		default:
			break;
		}
	}

	@Override
	public void loadResources() {
	}

	@Override
	public void loadScenes() {
	}

	@Override
	public void processMessage(Message message) {
		switch (message.getCMD()) {
		case CMD.S_LOGIN:
			S_LOGIN(message);
			break;
		case CMD.S_REGISTER:
			Log.d("register", "receive");
			boolean checkRegister = message.readBoolean();
			Log.d("register", "receive: " + checkRegister);
			String alert = message.readString();
			Log.d("register", "receive: " + alert);
			showToast(alert + " " + checkRegister);
			if (checkRegister) {
				run(new IAction() {
					@Override
					public void perform() {
						setContentView(layoutLogin);
						etAccount.setText(register_account);
						etPassword.setText(register_password);
					}
				});
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void onBackPressed() {
		if (connection != null) {
			connection.closeSocket();
		}
		connection = null;
		finish();
	}

	@Override
	public void disConnection() {
		// Xu ly phan ket noi that bai
		// showToast("Mat ket noi toi server");
	}
}
